<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy" content="media-src 'self' file: data: blob: filesystem:; media-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>AV&BV互转</title>
  </head>
  <body>
    <input id="vinp" placeholder="AV号/BV号">
    <button onclick="atb()">AV-->BV</button>
    <button onclick="bta()">BV-->AV</button>
    <input id="oinp" placeholder="输出">
  </body>
  <script>
    const { ipcRenderer } = require('electron')
    function atb() {
      var v=document.getElementById('vinp').value
      var httpRequest = new XMLHttpRequest()//第一步：创建需要的对象
        httpRequest.open('GET','https://api.bilibili.com/x/web-interface/view?aid='+v, true) //第二步：打开连接
        httpRequest.setRequestHeader("Content-type","application/json");//设置请求头
        httpRequest.send()//发送请求
        /**
         * 获取数据后的处理程序
         */
        httpRequest.onreadystatechange = function () {//请求后的回调接口，可将请求成功后要执行的程序写在其中
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
              try {
                var json = JSON.parse(httpRequest.responseText)
                if (json.data.bvid === undefined || json.data.bvid === null || json.data.bvid === "") {
                  document.getElementById('oinp').value = '失败3'
                }
                document.getElementById('oinp').value = json.data.bvid
              } catch (error) {
                document.getElementById('oinp').value = '失败2'
              }
            }else{
              document.getElementById('oinp').value = '失败1'
            }
        }
    }
    function bta() {
      var v=document.getElementById('vinp').value
      var httpRequest = new XMLHttpRequest()//第一步：创建需要的对象
        httpRequest.open('GET','https://api.bilibili.com/x/web-interface/view?bvid='+v, true) //第二步：打开连接
        httpRequest.setRequestHeader("Content-type","application/json");//设置请求头
        httpRequest.send()//发送请求
        /**
         * 获取数据后的处理程序
         */
        httpRequest.onreadystatechange = function () {//请求后的回调接口，可将请求成功后要执行的程序写在其中
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
              try {
                var json = JSON.parse(httpRequest.responseText)
                if (json.data.bvid === undefined || json.data.bvid === null || json.data.bvid === "") {
                  document.getElementById('oinp').value = '失败3'
                }
                document.getElementById('oinp').value = json.data.aid
              } catch (error) {
                document.getElementById('oinp').value = '失败2'
              }
            }else{
              document.getElementById('oinp').value = '失败1'
            }
        }
    }
    function dow(vdata) {
      
    }
  </script>
  <style>
    html,body{
      height: 100%;
      margin: 0;
      background-color:black;
    }
    button{
      margin: 0;
      width: 100%;
      height: 25%;
    }
    input{
      margin: 0;
      border: 0;
      padding: 0;
      width: 100%;
      height: 25%;
    }
  </style>
</html>
